package com.uchiha.nova.mainService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.uchiha.nova.core.model.entity.ChinaAreaEntity;
import com.uchiha.nova.core.model.vo.ChinaAreaVO;
import com.uchiha.nova.core.tool.api.R;
import com.uchiha.nova.core.tool.util.BeanUtil;
import com.uchiha.nova.mainService.cache.ChinaAreaCache;
import com.uchiha.nova.mainService.mapper.ChinaAreaMapper;
import com.uchiha.nova.mainService.service.IChinaAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 行政设置 服务实现类
 *
 * @author Chill
 */
@Service
@Slf4j
public class ChinaAreaServiceImpl extends ServiceImpl<ChinaAreaMapper, ChinaAreaEntity> implements IChinaAreaService {



    @Override
    public boolean deletePhysics(List<Long> ids) {
        ids.forEach(item-> this.baseMapper.deletePhysics(item));
        return true;
    }

    @Override
    public R treeLazyChinaArea(Integer id) {
        List<ChinaAreaEntity> list = this.list(new QueryWrapper<ChinaAreaEntity>().eq("parent_id",id));
        List<ChinaAreaVO> vos = list.stream().map(item->{
            ChinaAreaVO chinaAreaVO = BeanUtil.copy(item, ChinaAreaVO.class);
            Long a = this.count(new QueryWrapper<ChinaAreaEntity>().eq("parent_id",item.getId()));
            chinaAreaVO.setHasChildren(a.compareTo(0L)==1);
            return chinaAreaVO;
        }).collect(Collectors.toList());
        return R.data(vos);
    }

    @Override
    public List<ChinaAreaVO> tree() {
        return ChinaAreaCache.getChinaArea();
    }



}
